package test.org.offsync.util

import org.junit.runner.RunWith
import org.offsync.util.Window
import org.specs2._
import org.specs2.runner._

@RunWith(classOf[org.specs2.runner.JUnitRunner])
class SpecWindow extends org.specs2.mutable.SpecificationWithJUnit {
  "Array(0, 1, 2, 3) should" should {
    val array = Array(0, 1, 2, 3)
    "successfully construct Window" in {
      val window = Window to array
      "which cannot create subwindow from index -1" in {
        window.from(-1) must throwA[AssertionError]
      }
      "which can create subwindow from index 0" in {
        val window0 = window from 0
        "which cannot create subwindow until index -1" in {
          window0.until(-1) must throwA[AssertionError]
        }
        "which can create subwindow until index 0" in {
          val window00 = window0 until 0
          window00.length must_== 0
          window00(-1) must throwA[AssertionError]
          window00(0) must throwA[AssertionError]
          window00(1) must throwA[AssertionError]
        }
        "which can create subwindow until index 1" in {
          val window01 = window0 until 1
          window01.length must_== 1
          window01(-1) must throwA[AssertionError]
          window01(0) must_== 0
          window01(1) must throwA[AssertionError]
        }
        "which can create subwindow until index 2" in {
          val window02 = window0 until 2
          window02.length must_== 2
          window02(-1) must throwA[AssertionError]
          window02(0) must_== 0
          window02(1) must_== 1
          window02(2) must throwA[AssertionError]
        }
        "which can create subwindow until index 3" in {
          val window03 = window0 until 3
          window03.length must_== 3
          window03(-1) must throwA[AssertionError]
          window03(0) must_== 0
          window03(1) must_== 1
          window03(2) must_== 2
          window03(3) must throwA[AssertionError]
        }
        "which can create subwindow until index 4" in {
          val window04 = window0 until 4
          window04.length must_== 4
          window04(-1) must throwA[AssertionError]
          window04(0) must_== 0
          window04(1) must_== 1
          window04(2) must_== 2
          window04(3) must_== 3
          window04(4) must throwA[AssertionError]
        }
        "which cannot create subwindow until index 5" in {
          window0.until(5) must throwA[AssertionError]
        }
      }
      "which can create subwindow from index 1" in {
        val window1 = window from 1
        "which cannot create subwindow until index -1" in {
          window1.until(-1) must throwA[AssertionError]
        }
        "which can create subwindow until index 0" in {
          val window10 = window1 until 0
          window10(-1) must throwA[AssertionError]
          window10(0) must throwA[AssertionError]
          window10(1) must throwA[AssertionError]
        }
        "which can create subwindow until index 1" in {
          val window11 = window1 until 1
          window11(-1) must throwA[AssertionError]
          window11(0) must_== 1
          window11(1) must throwA[AssertionError]
        }
        "which can create subwindow until index 2" in {
          val window12 = window1 until 2
          window12(-1) must throwA[AssertionError]
          window12(0) must_== 1
          window12(1) must_== 2
          window12(2) must throwA[AssertionError]
        }
        "which can create subwindow until index 3" in {
          val window13 = window1 until 3
          window13(-1) must throwA[AssertionError]
          window13(0) must_== 1
          window13(1) must_== 2
          window13(2) must_== 3
          window13(3) must throwA[AssertionError]
        }
        "which cannot create subwindow until index 4" in {
          window1.until(4) must throwA[AssertionError]
        }
      }
      "which can create subwindow from index 2" in {
        val window2 = window from 2
        "which cannot create subwindow until index -1" in {
          window2.until(-1) must throwA[AssertionError]
        }
        "which can create subwindow until index 0" in {
          val window20 = window2 until 0
          window20(-1) must throwA[AssertionError]
          window20(0) must throwA[AssertionError]
          window20(1) must throwA[AssertionError]
        }
        "which can create subwindow until index 1" in {
          val window21 = window2 until 1
          window21(-1) must throwA[AssertionError]
          window21(0) must_== 2
          window21(1) must throwA[AssertionError]
        }
        "which can create subwindow until index 2" in {
          val window22 = window2 until 2
          window22(-1) must throwA[AssertionError]
          window22(0) must_== 2
          window22(1) must_== 3
          window22(2) must throwA[AssertionError]
        }
        "which cannot create subwindow until index 3" in {
          window2.until(3) must throwA[AssertionError]
        }
      }
      "which can create subwindow from index 3" in {
        val window3 = window from 3
        "which cannot create subwindow until index -1" in {
          window3.until(-1) must throwA[AssertionError]
        }
        "which can create subwindow until index 0" in {
          val window30 = window3 until 0
          window30(-1) must throwA[AssertionError]
          window30(0) must throwA[AssertionError]
          window30(1) must throwA[AssertionError]
        }
        "which can create subwindow until index 1" in {
          val window31 = window3 until 1
          window31(-1) must throwA[AssertionError]
          window31(0) must_== 3
          window31(1) must throwA[AssertionError]
        }
        "which cannot create subwindow until index 2" in {
          window3.until(2) must throwA[AssertionError]
        }
      }
      "which can create subwindow from index 4" in {
        val window4 = window from 4
        "which cannot create subwindow until index -1" in {
          window4.until(-1) must throwA[AssertionError]
        }
        "which can create subwindow until index 0" in {
          val window40 = window4 until 0
          window40(-1) must throwA[AssertionError]
          window40(0) must throwA[AssertionError]
          window40(1) must throwA[AssertionError]
        }
        "which cannot create subwindow until index 1" in {
          window4.until(1) must throwA[AssertionError]
        }
      }
      "which cannot create subwindow from index 5" in {
        window.from(5) must throwA[AssertionError]
      }
    }
  }
}
